import React,{useReducer} from 'react';
import {Button} from "antd";

const initialState = {
    num:0
}

const store = function (state,action){
    switch (action.type) {
        case 'plus':
            return {...state,num:state.num+1};
        case 'minus':
            return {...state,num:state.num-1};
    }
}

/**
 * useReducer
 * @returns {Element}
 * @constructor
 */
export const UseReducer = function () {

    const [state,dispatch] = useReducer(store,initialState);

    return (
        <>
            <div>
                {state.num}
            </div>
            <Button onClick={()=>{
                dispatch({
                    type:'plus'
                })
            }}>+</Button>
            <Button onClick={()=>{
                dispatch({
                    type:'minus'
                })
            }}>-</Button>
        </>
    )

};